package com.situ.mall.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.situ.mall.admin.entity.User;
import com.situ.mall.admin.service.IUserService;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Value("${file.path}")
    private String filePath;

    @GetMapping("/page")
    public String page() {
        return "user";
    }

    @GetMapping("/list")
    @ResponseBody
    public TableVO list(Integer page, Integer limit, String username, String name, String status) {
        return userService.selectList(page, limit, username, name, status);
    }

    @PostMapping("/save")
    @ResponseBody
    public void save(User user) {
        user.setAvatar(user.getAvatar().replace(filePath, ""));
        userService.saveOrUpdate(user);
    }

    @GetMapping("/check")
    @ResponseBody
    public User check(String username) {
        return userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
    }

    @GetMapping("/{id}")
    @ResponseBody
    public User info(@PathVariable Integer id) {
        return userService.getById(id);
    }

    @PostMapping("/delete")
    @ResponseBody
    public void delete(String ids) {
        userService.removeBatchByIds(Arrays.asList(ids.split(",")));
    }


}
